package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.com.elite.util.Util;

public class mainService extends HttpServlet {
	private static final Log log=LogFactory.getLog(mainService.class);
	
	public cn.com.elite.monitorBean monitorbean;

	/**
	 * Constructor of the object.
	 */
	public mainService() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String MONITORNO=(String)request.getSession().getAttribute("MONITORNO");
		log.debug("MONITORNO:"+MONITORNO);
		String sql="select * from MONITOR_CONFIG where MONITORNO="+MONITORNO;
		log.debug("sql:"+sql);
		try{
			List list=monitorbean.QuerySql(sql);
			if(list!=null&&list.size()>0){
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("interval", 3000);
				JSONArray jsonArr = new JSONArray();
				JSONArray jsonArr1 = new JSONArray();
				for(int i=0;i<list.size();i++){
					Map map=(Map)list.get(i);
					String MONITORTITLE=(String)map.get("MONITORTITLE");
					log.debug("MONITORTITLE:"+MONITORTITLE);
					String HTMLPATH=(String)map.get("HTMLPATH");
					log.debug("HTMLPATH:"+HTMLPATH);
					String INTERVALLEN=((BigDecimal)map.get("INTERVALLEN")).toString();
					log.debug("INTERVALLEN:"+INTERVALLEN);
					JSONObject jsonObjArr = new JSONObject(); 
					jsonObjArr.put("title", MONITORTITLE);  			         
					jsonObjArr.put("page", HTMLPATH); 
					jsonObjArr.put("size", 10); 
					jsonArr.put(jsonObjArr);
			        jsonArr1.put(Integer.parseInt(INTERVALLEN));
				}
				jsonObj.put("targets", jsonArr);
				jsonObj.put("targets_show_time", jsonArr1);
				JSONObject jsonObjArr2 = new JSONObject(); 
				jsonObjArr2.put("staff_id", "7B436A");  			         
				jsonObjArr2.put("my_rolegroup_id", "SYSTEM"); 
				jsonObjArr2.put("app_path", "/EliteGeneralWS_S_M"); 
				jsonObjArr2.put("GraphFontColor", "#000000"); 
				jsonObjArr2.put("GraphBackColor", "#FFFFFF"); 
//				jsonObjArr2.put("GraphBackColor", "transparent"); 
				jsonObjArr2.put("GraphBackPic", "/a.jpg"); 
//				jsonObjArr2.put("heighChartLength", 15); 
				jsonObj.put("params",jsonObjArr2);
				String returnJason=jsonObj.toString();
				log.debug("returnJason:"+returnJason);
		        request.setCharacterEncoding("UTF-8");
		        response.setContentType("text/html;charset=UTF-8");
		        PrintWriter out=response.getWriter();
		        if(returnJason.length()>0){
		        	 out.print(returnJason);	
		        }	
			}else{
				log.error("list.size()"+list.size());
			}
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
        ServletContext application;   
        WebApplicationContext wac;   
        application = getServletContext();   
        wac = WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        monitorbean = (cn.com.elite.monitorBean) wac.getBean("monitorBean");  
	}

}
